<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件名处理并保存到 Excel</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" rel="stylesheet">
    <style>
        /* 自定义样式，可根据需要调整 */
        body {
            font-family: 'Inter', sans-serif;
        }
    </style>
</head>

<body class="bg-gray-100 p-8">
    <div class="max-w-md mx-auto bg-white p-6 rounded-md shadow-md">
        <h1 class="text-2xl font-bold mb-4">选择文件夹并处理文件名</h1>
        <input type="file" id="folderInput" webkitdirectory directory multiple class="mb-4">
        <button id="processButton" class="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600">处理并保存到 Excel</button>
    </div>
    <script>
        document.getElementById('processButton').addEventListener('click', async () => {
            const input = document.getElementById('folderInput');
            const files = input.files;

            if (files.length === 0) {
                alert('请先选择一个文件夹！');
                return;
            }

            const fileNames = [];
            for (let i = 0; i < files.length; i++) {
                fileNames.push(files[i].name);
            }

            const processedData = fileNames.map(name => {
              const dotIndex = name.lastIndexOf('.');
                const extension = name.slice(dotIndex + 1);
                const nameWithoutExtension = name.slice(0, dotIndex);
                const secondPart = nameWithoutExtension.slice(-13);
                const firstPart = nameWithoutExtension.slice(0, -13);
                return [firstPart, secondPart, extension];
            });

            const worksheet = XLSX.utils.aoa_to_sheet(processedData);
            const workbook = XLSX.utils.book_new();
            XLSX.utils.book_append_sheet(workbook, worksheet, '文件名处理结果');
            XLSX.writeFile(workbook, 'processed_file_names.xlsx');
        });
    </script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
</body>

</html>
    